package com.changgou.goods.service.Impl;

import com.changgou.goods.dao.AlbumMapper;
import com.changgou.goods.service.AlbumService;
import com.changgou.pojo.Album;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AlbumServiceImpl
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-08-02
 * @Version V1.0
 **/
@Service
public class AlbumServiceImpl implements AlbumService {
    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public List<Album> findAll() {
        return albumMapper.selectAll();
    }

    @Override
    public Album findById(Long id) {
        return albumMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Album album) {
        albumMapper.insert(album);
    }

    @Override
    public void update(Album album) {
        albumMapper.updateByPrimaryKeySelective(album);
    }

    @Override
    public void delete(Long id) {
        albumMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Page<Album> search(Integer pageNum, Integer pageSize, Map<String, Object> searchMap) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = createExample(searchMap);
        Page<Album> albums = (Page<Album>) albumMapper.selectByExample(example);
        return albums;
    }

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Album.class);
        if (!CollectionUtils.isEmpty(searchMap)) {
            Example.Criteria criteria = example.createCriteria();
            String id = (String) searchMap.get("id");
            if (null != id) {
                criteria.andEqualTo("id", id);
            }
        }
        return example;
    }
}
